using UnityEngine;
using System.Collections;
using System.Reflection;

public class Manager : MonoBehaviour
{
	public static bool gameReset = false;
    public static bool Sound = false;
    public static bool Vibrate = false;
    public DifficultyManager diff = new DifficultyManager(); 
    //public GUIStyle myCustomGUIStyle;
    GameObject player;
    Rect rect = new Rect(0, 0, 120, 50);
        public DifficultyClass difficulty;
    public string testString = "test";
//    TextAsset txt;
    // Use this for initialization
	void OnApplicationQuit(){
		GameRestart();
	}

    void Start()
    {
      const BindingFlags flags = /*BindingFlags.NonPublic | */BindingFlags.Public | 
         BindingFlags.Instance | BindingFlags.Static;
		FieldInfo[] fields = typeof(Manager).GetFields(flags);
		foreach (FieldInfo fieldInfo in fields)
		{
		    Debug.Log("Obj: " + "Manager" + ", Field: " + fieldInfo.Name);
		}
        //PropertyInfo[] properties = typeof(Manager).GetProperties(flags);
        //foreach (PropertyInfo propertyInfo in properties)
        //{
        //    Debug.Log("Obj: " + "Manager" + ", Property: " + propertyInfo.Name);
        //}       
		GameObject city = GameObject.FindGameObjectWithTag("city").transform.GetChild(0).gameObject;
        if (PlayerPrefs.GetString("Diffculty") == "Boss")
        {
           city.SetActive(false);
        }
        //        print(Screen.height + "x" + Screen.width);
        player = GameObject.Find("JetFighter");
        
       

        //Time.timeScale = 1;
        //print(Application.platform);

//        txt = (TextAsset)Resources.Load("readme", typeof(TextAsset));
//
//        string content = txt.text;
		DifficultyManager();
        Vector2 convert = new Vector2(GameGUI.Screen.width / 2 - 280, GameGUI.Screen.height / 2 + 300);
        Vector2 flippedPoints = GUIUtility.ScreenToGUIPoint(new Vector2(convert.x, convert.y));
        //print(convert);
        //print(flippedPoints);
        ////    upgradeRect2 = new Rect(convert.x,Screen.height - convert.y, 120, 50);
    }

    // Update is called once per frame
    void Update()
    {

        //if (upgradeRect.Contains(Input.mousePosition))
        //{
        //    Debug.LogError("WORKS!");
        //}
        //Debug.Log(GameGUI.Screen.height / 2 + 300);


        //if (Input.touchCount > 0)
        //{

        //}


    }
    public static void GameRestart()
    {
		TouchKit.removeAllGestureRecognizers();
		UpgradeGUINew.upgradeEnabled = false;
		PauseMenu.pauseMenuEnabled = false;
        Time.timeScale = 1;
        Controller.shootSecondary = true;
	

        string startShip = PlayerPrefs.GetString("PlayerShip");
		string difficulty = PlayerPrefs.GetString("Difficulty");
        string beatGame = PlayerPrefs.GetString("BeatGame");
        PlayerPrefs.DeleteAll();
        PlayerPrefs.SetString("BeatGame", beatGame);
        PlayerPrefs.SetString("PlayerShip", startShip);
		PlayerPrefs.SetString("Difficulty", difficulty);
        Application.LoadLevel(1);
    }



    public static void MainMenu()
    {
        TouchKit.removeAllGestureRecognizers();
        UpgradeGUINew.upgradeEnabled = false;
        PauseMenu.pauseMenuEnabled = false;
        Time.timeScale = 1;
        Controller.shootSecondary = true;
        string startShip = PlayerPrefs.GetString("PlayerShip");
        string difficulty = PlayerPrefs.GetString("Difficulty");
        string beatGame = PlayerPrefs.GetString("BeatGame");
        PlayerPrefs.DeleteAll();
        PlayerPrefs.SetString("BeatGame",beatGame);
        PlayerPrefs.SetString("PlayerShip", startShip);
        PlayerPrefs.SetString("Difficulty", difficulty);
        StartGame.gameStart = false;
        StartGame.selectLevels = false;
		GameCenterSingleton.Instance.ResetAchievements();
        Application.LoadLevel(0);

    }
    public static void Win()
    {
        PlayerPrefs.SetString("BeatGame", "Yes");
        Application.LoadLevel(4);
    }
    public static void WavePrep()
    {

    }
    public static void WavePause()
    {
        Time.timeScale = 0;
        UpgradeGUINew.onStart = true;
        Controller.shootSecondary = false;
    }
    public static void WaveResume(int currHealthBoost)
    {

        Controller.currHealthBoost = currHealthBoost;
        WaveResume(false);
    }
    public static void WaveResume(bool timeScale)
    {
        UpgradeGUINew.upgradeEnabled = false;
        float timeDelay = Time.time;
        Controller.onStart = true;
        Buildings.refresh = true;
        if (timeScale)
        {
            Time.timeScale = 1;
            Controller.shootSecondary = true;
        }
    }


    void OnGUI()
    {

		
		
        
        bool upgrades = false;

        //if(Hodor.turnonwingui){
        //    Time.timeScale = 0;
        Rect upgradeRect = new Rect(Screen.width / 2 - 280, Screen.height / 2 + 300, 120, 50);
        Rect touchUpgrade = upgradeRect;
        touchUpgrade.y = Screen.height - touchUpgrade.y;

        Rect resetRect = (new Rect(Screen.width / 2 - 280, Screen.height / 2 + 350, 90, 50));
        Rect touchReset = resetRect;
        touchReset.y = Screen.height - touchReset.y;

        //if (Input.touchCount > 0)
        //{

        //    if (touchReset.Contains(Input.touches[0].position) && Input.touches[0].phase == TouchPhase.Began)
        //    {
        //        Controller.shootSecondary = false;
        //        gameReset = true;
        //    }
        //    if (touchUpgrade.Contains(Input.touches[0].position) && Input.GetTouch(0).phase == TouchPhase.Began)
        //    {
        //        Controller.shootSecondary = false;
        //        upgrades = true;
        //    }
        //}
        //if (GUI.Button(upgradeRect, "Upgrades",myCustomGUIStyle) || upgrades)
        //{
        //    //Application.LoadLevel(2);

        //    UpgradeGUINew.upgradeEnabled = true;
        //    WavePause();
        //}

        //Rect resetRect = new Rect(GameGUI.Screen.width / 2 - 280, Screen.height - (GameGUI.Screen.height / 2 + 300), 120, 50);

        //print(upgradeRect2);

        if (Input.GetKeyDown(KeyCode.Z)) //GUI.Button(resetRect, "Game Reset",myCustomGUIStyle) || gameReset)
        {
            gameReset = false;
            //PlayerPrefs.DeleteAll();
            //Application.LoadLevel(1);
            GameRestart();
        }
        //}
    }
    [System.Serializable]
    public class DifficultyClass
    {
        public string currentDifficulty = "Enter Difficulty Here";
        public int enemyHealthAdder = 0;
        public float enemyHealthMultiplier = 0f;
        public int enemyDamageAdder = 0;
        public float enemyDamageMultiplier = 0f;
        public float enemyHorizontalSpeedMultiplier = 0f;
        public float enemyVerticalSpeedMultiplier = 0f;
        public int enemyCurrencyDropAdder = 0;

    }


    public class DifficultyClass2
    {
        public string currentDifficulty = "Enter Difficulty Here";
        public int enemyHealthAdder = 0;
        public float enemyHealthMultiplier = 0f;
        public int enemyDamageAdder = 0;
        public float enemyDamageMultiplier = 0f;
        public float enemyHorizontalSpeedMultiplier = 0f;
        public float enemyVerticalSpeedMultiplier = 0f;
        public int enemyCurrencyDropAdder = 0;
    }

    void DifficultyManager()
    {
		Transform waves = GameObject.FindGameObjectWithTag("WaveManager").transform;
		int activeCount = 0;
		foreach(Transform child in waves)
		{
			if(child.name == PlayerPrefs.GetString("Difficulty")+"_Wave")
			{
				child.gameObject.SetActive(true);
				activeCount++;
			}
			else 
			{
				child.gameObject.SetActive(false);
			}
		}
		if(activeCount == 0)
		{
			//Debug.LogError("Difficulty Select Error");
		}
		
//		switch(PlayerPrefs.GetString("Difficulty"))
//		{
//			case "easy":
//				waves
//				
//		}
			
    }
}
